import 'package:get/get.dart';
import 'package:safetymonitor/app/components/custom_select_function.dart';
import 'package:safetymonitor/app/components/toast_widget.dart';
import 'package:safetymonitor/app/core/env/env.dart';
import 'package:safetymonitor/app/core/http/http.dart';
import 'package:safetymonitor/app/core/utils/interface.dart';
import 'package:safetymonitor/app/core/utils/user_manager.dart';
import 'package:safetymonitor/app/data/entities/home/warning/alarm_resp_entity.dart';
import 'package:safetymonitor/modules/home/alarm_warning/no_completed/controller.dart';
///处置（蓝色和黄色）
class DisposeController extends GetxController {
  /// 当前选中的标题索引，0：基本信息 1:隐患整改
  RxInt currentIndex = 0.obs;

  /// 实体数据
  AlarmRespEntity entity = Get.arguments as AlarmRespEntity;

  /// 列表控制器
  final listController = Get.find<NoCompletedController>();
  /// （登陆人id）
  int? latestDistributionHighId = UserManager.getUser()!.id;
  ///  负责人
  int? affiliateCompanyAssignHighId;
  String affiliateCompanyAssignHighIdName = '';
  //-------------------------------处置人--------------------------------------------------
  ///报警原因
  String reasonAlarm='';
  /// 报警描述
  String alarmDescription='';
  /// 现场处理人
  int? onSiteHandlerId;
  String onSiteHandlerName='';
  /// 所属部门
  int? departmentBelongsId;
  String departmentBelongsIdName='';
  /// 现场处理时间
  String onSiteProcessingTime='';
  /// 处置措施
  String disposalMeasures='';
  /// 处置附件
  String disposalAttachments='';
  /// title选择切换
  void selectTitle(int index) {
    currentIndex.value = index;
  }

  /// 确定提交
  Future<void> confirm() async {
    if (reasonAlarm.trim().isEmpty) {
      AppToast.showError('请输入报警原因');
      return ;
    }
    if (alarmDescription.trim().isEmpty) {
      AppToast.showError('请输入报警描述');
      return ;
    }
    if (onSiteHandlerName.trim().isEmpty) {
      AppToast.showError('请输入现场处理人');
      return ;
    }
    if (departmentBelongsIdName.trim().isEmpty) {
      AppToast.showError('请输入所属部门');
      return ;
    }
    if (onSiteProcessingTime.trim().isEmpty) {
      AppToast.showError('请输入现场处理时间');
      return ;
    }
    // if (affiliateCompanyAssignHighIdName.trim().isEmpty) {
    //   AppToast.showError('请输入负责人');
    //   return false;
    // }
    if (disposalMeasures.trim().isEmpty) {
      AppToast.showError('请输入处置措施');
      return ;
    }
    if (disposalAttachments.trim().isEmpty&& DartDefines.appInstallChannel != AppInstallChannel.kaifa) {
      AppToast.showError('请输入处置附件');
      return ;
    }
    //如果是处置这里还要在掉另一个接口（老版本的接口）
    chuZhiConfirm();
    // 调用接口
    final res = await HttpUtil()
        .generatePutRequest(
      Interface.alarmUpdate,
      data:{
        'id': entity.id,
        'click': entity.click,
        'latestOperationStatus': '8',
        'latestDisposalId': latestDistributionHighId, //处置人id 也是登陆人
        'alarmLevel': entity.alarmLevel,

        'sceneDisposalPersonId': onSiteHandlerId, //现场处理人
        'ascriptionDeptId': departmentBelongsId, //所属部门
        'sceneDisposalTime': onSiteProcessingTime, //现场处理时间
        'sceneDisposalTimeStr': onSiteProcessingTime, //现场处理时间
        'undertakePersonId': latestDistributionHighId, //负责人

        //新增字段（记录原因）老接口合并参数
        'reasonType': reasonAlarm, //处置报警原因类型
        'reasonAnalysis': alarmDescription, //处置报警原因分析
        // 'sceneDisposalPersonId':sceneDisposalPersonId, //现场处理人id
        // 'ascriptionDeptId':ascriptionDeptId, //所属部门id
        // 'sceneDisposalTime':sceneDisposalTime, // 现场处理时间
        // 'undertakePersonId':undertakePersonId, // 负责人id
        'disposalMeasures': disposalMeasures, // 处置措施
        'disposalMeasuresAnnex': disposalAttachments, //  处置措施附件
      },
    )
        .execute(isShowLoading: true);
    res.onSuccess((result) {
      Get.back(closeOverlays: true);
      AppToast.showSuccess('提交成功');
    });
  }
  /// 发现时间选择
  void showDatePicker() {
    CustomSelectFunction.selectDateTime(
      onCallBack: (String value) {
        onSiteProcessingTime =value;
        update();
      },
    );
  }

  /// 老版本处置时的提交（处置消警操作）
  Future<void> chuZhiConfirm() async {
    // 附件信息
    final res = await HttpUtil().generatePutRequest(
      Interface.putDisposeAlarm,
      data: {
        'id': entity.id,
        // 'type': 2,
        'reasonType': reasonAlarm,//报警原因
        'reasonAnalysis': alarmDescription,//报警描述
        'description': disposalMeasures,//处置措施
        'disarmingDescriptionAnnex': disposalAttachments,//附件
      },
    ).execute(isShowLoading: true);
    res.onSuccess((result) {
      AppToast.showSuccess('处置成功!');
    });
  }
}
